package com.aurora.oa.work.presenter;


import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnSendLogListener;
import com.aurora.oa.listener.OnSendPatrollogListener;
import com.aurora.oa.listener.OnUploadAttachmentListener;
import com.aurora.oa.message.bean.ResponseFileID;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.work.bean.SendLogBean;
import com.aurora.oa.work.bean.SendPatrollogBean;
import com.aurora.oa.work.view.WriteLogDetailActivityView;
import com.aurora.oa.work.view.WritePatrollogActivity;
import com.aurora.oa.work.view.WritePatrollogActivityView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Administrator on 2017/05/02
*/

public class WritePatrollogActivityPresenterImpl implements WritePatrollogActivityPresenter{

    WritePatrollogActivityView view;
    RequestHttpModel model;
    List<File> files;
    List<String> fileIDs;
    SendPatrollogBean logData;
    public WritePatrollogActivityPresenterImpl(WritePatrollogActivityView writePatrollogActivity) {
        view = writePatrollogActivity;
        model = new RequestHttpModelImpl();
    }

    @Override
    public void sendLog(){
        //获取要发送的数据
        logData= view.getLogData();
        //为空就不发送消息
        if(checkLogNull()){
            return;
        }

        view.showProgress();
        files = view.getImageFiles();
        if(files.size()==0){
            //发送没有图片的数据
            model.sendPatrollog(logData, new OnSendPatrollogListener() {
                @Override
                public void onSendPatrollogSuccess(HttpResult result) {
                     view.onLogSendSuccess();

                }

                @Override
                public void onSendPatrollogFail(String message) {
                    view.onLogSendFail(message);
                }

            });
            return;
        }
        fileIDs = new ArrayList<>();
        //先循环发送图片
        for(int i = 0 ;i<files.size();i++){
            model.uploadAttachment(files.get(i), new OnUploadAttachmentListener() {
                @Override
                public void onUploadAttachmentSuccess(ResponseFileID attachId) {
                    fileIDs.add(attachId.getMsg());
                    //所有图片上传完成
                    if(fileIDs.size()==files.size()){
                        //开始上传数据
                        logData.attachmentids = StringUtils.getStringFromList(fileIDs);
                        model.sendPatrollog(logData, new OnSendPatrollogListener() {
                            @Override
                            public void onSendPatrollogSuccess(HttpResult result) {
                                view.onLogSendSuccess();
                            }

                            @Override
                            public void onSendPatrollogFail(String message) {
                                view.onLogSendFail(message);
                            }
                        });
                    }

                }

                @Override
                public void onUploadAttachmentFail(String message) {
                    view.onLogSendFail(message);
                }
            });
        }

    }

    //非空校验
    private boolean checkLogNull() {

        if(StringUtils.isNullString(logData.shopname)){
            view.onLogSendFail("超市名称不能为空");
            return true;
        }
        if(StringUtils.isNullString(logData.sales)){
            view.onLogSendFail("营业额不能为空");
            return true;
        }
        return false;
    }
}